#/usr/bin/python

from util import Util
from gen_java import *
from gen_sqltable import *
from gen_service import *
from gen_daomap import *
import sys
import re
import os

class Parser():
    def __init__(self):
        self.java_parser=JavaParser()
        self.sql_parser=SQLParser()
        self.dao_parser=DaoParser()
        self.service_parser=ServiceParser()
        self.dir="/" 
        self.util=Util()
        self.parse_table={
           'package':self.parser_package,
           'domain':self.parser_domain,
           'item':self.parser_item,
           'dao':self.parser_dao,
           'interface':self.parser_interface
        }

    def parser_item(self,sentences):
        java_file_doc=self.java_parser.run_parser(self.package,self.domain,sentences)
        java_file=open(self.dir+self.domain+".java","w+")
        java_file.write(java_file_doc)
        java_file.close()

        self.item_sentence=sentences

        tablename=self.util.javaid_to_sqlid(self.domain)
        create_doc=sql_file_doc=self.sql_parser.run_parser(tablename,sentences)
        create_file=open(self.dir+tablename+".sql","w+")
        create_file.write(create_doc)
        create_file.close()

    def parser_dao(self,sentences):
        import pdb
        #pdb.set_trace()
        dao_java_doc=self.dao_parser.run_parser(self.domain,sentences)
        dao_file=open(self.dir+"I"+self.domain+"Dao.java","w+")
        dao_file.write(dao_java_doc)
        dao_file.close()

        javamap=self.java_parser.get_java_table(self.package,self.domain,self.item_sentence)
        daomap=self.dao_parser.get_dao_table(self.domain,sentences)
        tablemap=MapTable(javamap,daomap)
        map_file_doc=tablemap.gen_map()
        map_file=open(self.dir+self.util.domain_to_resultTitle(self.domain),"w+")
        map_file.write(map_file_doc)
        map_file.close()
        
    def parser_interface(self,sentences):
        self.service_parser.init(self.package,self.domain,sentences)
        service_file_doc=self.service_parser.gen_service()
        service_file=open(self.dir+"I"+self.domain+"Service","w+")
        service_file.write(service_file_doc)
        service_file.close()

        interface_file_doc=self.service_parser.gen_interface()
        interface_file=open(self.dir+self.domain+"ServiceImpl","w+")
        interface_file.write(interface_file_doc)
        interface_file.close()

    def parser_file(self,filename):
        files=open(self.dir+filename,"r+")
        document=files.read()
        files.close()
        return self.parser(document)

    def parser_domain(self,domain):
        self.dir="./"+domain+"/"
        if not os.path.exists(self.dir):
            os.mkdir(self.dir)
        self.domain=domain

    def parser_package(self,package):
        self.package=package
        
    def parser(self,filename):
        f=open(filename,"r+")
        document=str(f.read())
        document=document.replace("\n","")
        sentences=document.split(";")
        for sentence in sentences:
            if re.sub('\s','',sentence)== "":
                continue
            id=re.sub('\s','',sentence.split(":")[0])
            interface=self.parse_table.get(id)
            if interface!=None:
                replace_str=sentence.split(":")[0]
                sentence=sentence.replace(replace_str+":","")
                print(interface.__name__+":"+sentence)
                interface(sentence)        
            else:
                print("can't parser :"+sentence+" id:"+id)

if __name__=="__main__":
   parse=Parser()
   if sys.argv.__len__()<=1:
       parse.parser("Default")
   else:
       parse.parser(sys.argv[0])


